package cn.dnf.product.service.impl;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.service.IBaiduAiAuditService;
import cn.dnf.basic.service.impl.BaseServiceImpl;
import cn.dnf.pet.domain.PetDetail;
import cn.dnf.product.domain.Product;
import cn.dnf.product.domain.ProductDetail;
import cn.dnf.product.mapper.ProductDetailMapper;
import cn.dnf.product.mapper.ProductMapper;
import cn.dnf.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void onSale(List<Long> ids) {
        Date date = new Date();
        productMapper.onSale(ids,date);
    }

    @Override
    public void offSale(List<Long> ids) {

        Date date = new Date();

        productMapper.offSale(ids,date);
    }


    @Override
    public void remove(Long id) {

        super.remove(id);
        //详情
        productDetailMapper.removeByProductId(id);
    }

    @Override
    public void update(Product product) {
        Boolean testBoolean = baiduAiAuditService.textAudit(product.getName());


        Boolean imgBoolean = baiduAiAuditService.imageAudit(product.getResources());

        if(testBoolean&&imgBoolean){
            productMapper.update(product);

            productDetailMapper.update(product.getProductDetail());
        }else{
            throw new BusinessException("信息不合规,请重新录入");
        }


    }

    @Override
    public void save(Product product) {

        Boolean testBoolean = baiduAiAuditService.textAudit(product.getName());


        Boolean imgBoolean = baiduAiAuditService.imageAudit(product.getResources());


        if(product.getResources()!=""){
            if(testBoolean&&imgBoolean){
                productMapper.save(product);
                ProductDetail productDetail = product.getProductDetail();
                productDetail.setProduct_id(product.getId());
                productDetailMapper.save(productDetail);
            }else{
                throw new BusinessException("信息不合规,请重新录入");
            }
        }else{
            if(testBoolean){
                productMapper.save(product);
                ProductDetail productDetail = product.getProductDetail();
                productDetail.setProduct_id(product.getId());
                productDetailMapper.save(productDetail);
            }else{
                throw new BusinessException("信息不合规,请重新录入");
            }
        }



    }
}
